package main

import (
	"fmt"
	"identity/controller/identityController"
	"mime"
	"strconv"

	"github.com/gin-contrib/static"
	"github.com/gin-gonic/gin"
)

func main() {
	fmt.Printf("identityapp start")
	host := gin.Default()

	// section 终结点
	// setp 1: 静态目录
	_ = mime.AddExtensionType(".js", "application/javascript")
	host.Use(static.Serve("/", static.LocalFile("./www", true)))

	// step 2: api
	api_host := host.Group("/api")

	// step 1:
	api_host.POST("/addIdentity", func(c *gin.Context) {
		identityController := identityController.New()
		// step 1.2： 获取请求信息
		name := c.PostForm("name")
		desc := c.PostForm("desc")
		c.JSON(200, identityController.AddIdentity(name, desc))
	})
	// step 2：
	api_host.GET("/getIdentity", func(c *gin.Context) {
		identityController := identityController.New()
		// step 2.1： 获取请求信息
		idStr, e2 := c.GetQuery("id")
		id, e1 := strconv.ParseInt(idStr, 10, 64)
		// step 3：校验
		if !(e1 == nil && e2) {
			c.JSON(200, gin.H{"message": "参数不正确，请重试"})
			return
		}
		c.JSON(200, identityController.GetIdentity(id))
	})
	// step 3：配置信息
	ip := "127.0.0.1"
	port := "8080"
	// step end： 服务启动
	//服务器要监听的主机地址和端口号
	address := fmt.Sprintf("%s:%s", ip, port)
	host.Run(address)
}
